import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/test',
    name: 'routeName',
    component: () => import('../components/Authorizition.vue'),
  },
  {
    redirect:'/managementsystem/statistics',
    path: '/managementsystem',
    name: '/ManagementSystem',
    children:[
      {
        path:'statistics', //统计路由分项1
        name:'Statistics',
        component: () => import('../views/Statistics'),
      },
      {
        path:'hxlist', //统计路由分项1
        name:'Hxlist',
        component: () => import('../views/Hxlist.vue'),
      }

    ],
    component: () => import('../views/ManagementSystem.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path ==='/login')return next()
  const tokenStr = window.localStorage.getItem('token')
  if(!tokenStr)return next('/login')
  next()
})

export default router
